<template>
  <div>
    <el-container>
      <el-header>
        <el-breadcrumb separator=">">
          <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
          <el-breadcrumb-item>商品管理</el-breadcrumb-item>
          <el-breadcrumb-item>添加商品</el-breadcrumb-item>
        </el-breadcrumb>
      </el-header>
      <el-main>
        <el-alert title="添加商品信息 " center type="info" :closable="false">
        </el-alert>
        <el-steps :space="200" :active="active" finish-status="success">
          <el-step title="基本信息"></el-step>
          <el-step title="商品参数"></el-step>
          <el-step title="商品属性"></el-step>
          <el-step title="商品图片"></el-step>
          <el-step title="商品内容"></el-step>
          <el-step title="完成"></el-step>
        </el-steps>
        <el-tabs
          :tab-position="tabPosition"
          style="height: 100%"
          v-model="table"
          :before-leave="beforeLeave"
          @tab-click="num(table)"
        >
          <el-tab-pane name="0" label="基本信息">
            <el-form
              :model="ruleForm"
              status-icon
              :rules="rules"
              ref="ruleForm"
              label-width="100px"
              class="demo-ruleForm"
            >
              <el-form-item label="商品名称" prop="goods_name">
                <el-input
                  type="username"
                  v-model="ruleForm.goods_name"
                  autocomplete="off"
                ></el-input>
              </el-form-item>
              <el-form-item label="商品价格" prop="goods_price">
                <el-input
                  type="goods_price"
                  v-model="ruleForm.goods_price"
                  autocomplete="off"
                ></el-input>
              </el-form-item>
              <el-form-item label="商品重量" prop="goods_weight">
                <el-input
                  type="goods_weight"
                  v-model="ruleForm.goods_weight"
                  autocomplete="off"
                ></el-input>
              </el-form-item>
              <el-form-item label="商品数量" prop="goods_number">
                <el-input
                  type="goods_number"
                  v-model="ruleForm.goods_number"
                  autocomplete="off"
                ></el-input>
              </el-form-item>
              <el-form-item label="商品分类" prop="list">
                <el-cascader
                  style="width: 100%"
                  @change="handleChange"
                  :options="options"
                  :props="{ label: 'cat_name', value: 'cat_id' }"
                  v-model="value"
                ></el-cascader>
              </el-form-item>
            </el-form>
          </el-tab-pane>
          <el-tab-pane name="1" label="商品参数">{{ ruleForm }}</el-tab-pane>
          <el-tab-pane name="2" label="商品属性">商品属性</el-tab-pane>
          <el-tab-pane name="3" label="商品图片"> </el-tab-pane>
          <el-tab-pane name="4" label="商品内容">
            <el-form label-width="80px">
              <el-form-item label="内容">
                <quillEditor
                  v-model="ruleForm.goods_introduce"
                  style="height: 200px"
                ></quillEditor>
              </el-form-item>
              <el-form-item>
                <el-button @click="add">添加商品</el-button></el-form-item
              >
            </el-form>
          </el-tab-pane>
        </el-tabs>
      </el-main>
    </el-container>
  </div>
</template>
<script>

import 'quill/dist/quill.core.css'
import 'quill/dist/quill.snow.css'
import 'quill/dist/quill.bubble.css'

import { quillEditor } from 'vue-quill-editor'


import { getcategoriesApi, addgoodsApi } from '@/api/goods'
export default {
  name: 'goodsAdd',
  components: {
    quillEditor
  },
  data () {
    return {
      active: 0,
      tabPosition: 'left',
      options: [],
      value: '',
      table: '',
      ruleForm: {
        goods_name: "",
        goods_cat: "",
        goods_price: '',
        goods_number: '',
        goods_weight: '',
        goods_introduce: "abc",
        
      }, rules: {
        goods_name: [
          { required: true, message: '用户名不能为空', trigger: 'blur', }
        ],
        goods_price: [
          { required: true, message: '用户名不能为空', trigger: 'blur', }
        ],
        goods_weight: [
          { required: true, message: '用户名不能为空', trigger: 'blur', }
        ],
        goods_number: [
          { required: true, message: '用户名不能为空', trigger: 'blur', }
        ],
      },

    }
  },
  async created () {
    const res = await getcategoriesApi()
    console.log(res, 909090909090)
    this.options = res.data
  },
  computed: {

  },
  methods: {
    //校验
    beforeLeave (activeName, oldActiveName) {
      this.activeName = activeName
      if (oldActiveName === undefined) return true
      if (this.ruleForm.goods_name !== '' && this.value.length > 0) {
        this.active = parseInt(activeName)
        return true
      } else {
        this.$message({
          type: 'error',
          message: '内容不能为空'
        })
        return false
      }
    },
    handleChange (value) {
      console.log(value);
      this.ruleForm.goods_cat = value.join(',')
    },
    num (table) {
      console.log(table, 9999)

      this.active = Number(table)
    },
    async add () {
      const res = await addgoodsApi(this.ruleForm)
      console.log(res)

      if (res.meta.msg == '') {
        this.$message({
          message: '添加失败',
          type: 'warning'
        });
        return
      } else if (res.meta.msg == '创建商品成功') {
        this.$message({
          message: '创建商品成功',
          type: 'success'
        });
      }

    },

  }
}
</script>
<style lang='scss'  scoped>
</style>
